package petrus.utils 
{
	import com.greensock.easing.Back;
	import com.greensock.easing.Linear;
	import com.greensock.TweenMax;
	import eric.FolioEvent;
	import flash.display.NativeMenu;
	import flash.display.Shape;
	import flash.display.Sprite;
	
	/**
	 * ...
	 * @author Benoit Petrus
	 */
	public class PartialCircle extends Sprite 
	{
		
		public var degree:Number 				= 0;	
		public var percent : Number 			= 0;		
		public var onComplete:Function;
		private var degChange:Number 			= 10; 
		private var shape:Shape;
		private var _radius:Number;
		private var _color:Number;
		private var _alpha:Number;
		private var _counter:Boolean;
		
		
		public function PartialCircle(r :Number = 60, c : Number = 0xFFFFFF, a : Number = 1 , rotate:Number = 0 , counter : Boolean =true  ) 
		{		
			_counter = counter
			_alpha = a;
			_color = c;
			_radius = r;
			
			if(counter)rotation = rotate+180;
			if(!counter)rotation = rotate-90;
			shape = new Shape();			
			addChild(shape);
			
			
		}
		
		public function update(_percent:Number):void 
		{
			//trace("[PartialCircle.update] ");
			TweenMax.killTweensOf(this)
			TweenMax.to(this, 1.3, { percent:  _percent , ease : Linear.easeNone,
									onUpdate : draw  , onComplete : unBounce   } );			
		}
		
		public function bounce():void 
		{
			shape.scaleX = shape.scaleY = .4
			TweenMax.to(shape, .7, {scaleX : 1  , scaleY : 1 , ease : Back.easeOut });
		}
		
		private function unBounce():void 
		{
			//trace("[PartialCircle.unBounce] ");
			TweenMax.delayedCall(.4 , onHided)
			TweenMax.to(shape, .9, {scaleX : .3  , scaleY : .3 , ease : Back.easeInOut ,  delay : 0});
			
		}
		
		private function onHided():void 
		{
			trace("[PartialCircle.onHided] ");
			onComplete.call()
			percent = 0
			degree = 0
			draw()
		}
		
		private function test():void 
		{			
			x = y  =400
		}
		
		// DRAW CLOCK WISE OR CCW depending on _counter
		private function draw():void 
		{
			//trace("[PartialCircle.draw] "+int(degree) , percent);
			degree = 360 * percent
			initDraw()
				
				
			for (var i:int = 0; i <= degree; i++) 
			{			
				var _x :Number =  _radius*Math.cos(i*Math.PI/180)
				var _y :Number = - _radius*Math.sin(i*Math.PI/180)
				_counter ? shape.graphics.lineTo(_y, _x) : shape.graphics.lineTo(_x, _y)			
			}
			
			shape.graphics.endFill();
		}

	
		private function initDraw():void 
		{
			var radianAngle:Number = degree*Math.PI/180.0; 
			shape.graphics.clear();
			shape.graphics.moveTo(0,0);
			shape.graphics.beginFill(_color,alpha);
		}
	}
}